package com.wjr.braveheart.utils;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;

import java.util.Stack;

/**
 * Activity的统一管理类
 */

public class ActivityManagerUtils {
    private static volatile ActivityManagerUtils sInstance;
    private static Stack<Activity> sActivityStack;

    private ActivityManagerUtils() {
    }

    public static ActivityManagerUtils getInstance() {
        if (sInstance == null) {
            synchronized (ActivityManagerUtils.class) {
                if (sInstance == null) {
                    sInstance = new ActivityManagerUtils();
                }
            }
        }
        return sInstance;
    }

    /**
     * 绑定 Activity 添加到集合统一管理
     *
     * @param activity
     */
    public void attachActivity(Activity activity) {
        if (sActivityStack == null) {
            sActivityStack = new Stack<>();
        }
        if (sActivityStack.contains(activity)) {
            return;
        }
        sActivityStack.add(activity);
    }

    /**
     * 解除 Activity 绑定，移除统一管理
     *
     * @param activity
     */
    public void detachActivity(Activity activity) {
        if (sActivityStack != null && sActivityStack.size() > 0) {
            int size = sActivityStack.size();
            for (int i = 0; i < size; i++) {
                // 遍历所有的 subscriptions ，逐一移除
                Activity equalActivity = sActivityStack.get(i);
                if (equalActivity.getClass().getName() == activity.getClass().getName()) {
                    sActivityStack.remove(i);
                    i--;
                    size--;
                }
            }
        }
    }

    /**
     * Gets the current Activity (stack last pressed into)
     */
    public Activity currentActivity() {
        Activity activity = sActivityStack.lastElement();
        return activity;
    }

    /**
     * The finish of the specified Activity
     */
    public void finishActivity(Activity activity) {
        if (activity != null && sActivityStack != null && sActivityStack.size() > 0) {
            int size = sActivityStack.size();
            for (int i = 0; i < size; i++) {
                // 遍历所有的 subscriptions ，逐一移除
                Activity equalActivity = sActivityStack.get(i);
                if (equalActivity.getClass().getName() == activity.getClass().getName()) {
                    sActivityStack.remove(i);
                    equalActivity.finish();
                    i--;
                    size--;
                }
            }
        }
    }

    /**
     * End of the specified class name of the Activity
     */
    public void finishActivity(Class<? extends Activity> cls) {
        if (cls != null && sActivityStack != null && sActivityStack.size() > 0) {
            int size = sActivityStack.size();
            for (int i = 0; i < size; i++) {
                // 遍历所有的 subscriptions ，逐一移除
                Activity equalActivity = sActivityStack.get(i);
                if (equalActivity.getClass().getName() == cls.getName()) {
                    sActivityStack.remove(i);
                    equalActivity.finish();
                    i--;
                    size--;
                }
            }
        }
    }

    /**
     * Finish all the Activity
     */
    public void finishAllActivity() {
        for (int i = 0, size = sActivityStack.size(); i < size; i++) {
            if (null != sActivityStack.get(i)) {
                sActivityStack.get(i).finish();
            }
        }
        sActivityStack.clear();
    }

    /**
     * Exit the application
     */
    @SuppressWarnings("deprecation")
    public void AppExit(Context context) {
        try {
            finishAllActivity();
            ActivityManager activityMgr = (ActivityManager) context
                    .getSystemService(Context.ACTIVITY_SERVICE);
            activityMgr.restartPackage(context.getPackageName());
            System.exit(0);
            android.os.Process.killProcess(android.os.Process.myPid());
        } catch (Exception e) {

        }
    }
}
